import { setToken, getToken, removeToken } from '@/utils/auth'
import { logout, getProfile, getProfile2 } from '@/api/user.js'
export default {
  namespaced: true,
  state: {
    token: getToken() || null,
    userInfo: {}
  },
  mutations: {
    // 添加token
    increment(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    // 删除token
    removeTokenAndUserInfo(state) {
      state.token = null
      removeToken()
      state.userInfo = {}
    },
    addUserInfo(state, newInfo) {
      state.userInfo = newInfo
    },
    // 删除用户信息
    reomveUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async doLogin(context, data) { // 请求token
      const res = await logout(data)
      context.commit('increment', res.data)
    },
    async getUserProfile(context) { // 请求用户信息
      const res = await getProfile()
      console.log(res.data)
      const rs = await getProfile2(res.data.userId)
      console.log(rs, ' ******')
      context.commit('addUserInfo', res.data)
      context.state.userInfo = { ...context.state.userInfo, ...rs.data }
    }
  }
}
